<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" src="./js/jquery-1.10.2.js"></script>
<script type="text/javascript">


 $(document).ready(function() {
	//id output 에 대한 객체가 계속 생기기 때문에 사용
	$('#output').empty();
	
	fn_searchList();
	
	$('#save').on('click', function(){

		fn_dupValidate();

    });
  
	var strValue = "";
	$('#delete').on('click', function(){
		$("input:checked[name='chk']:checked").each(function(){
			if(this.checked){
				strValue += this.value;
				
			}
		});
		
		var form = document.form;
		form.strdelete.value = strValue; 
		form.action = "roomDeleteMng.do";
		form.submit();
		
	 });

	$('#search').on('click', function(){
		fn_searchList();
    });
	
});

</script>
<script>

    function fn_searchList(){

    	$('#output').empty();

    	var frm = jQuery("form[name=form]");
    	var dataString = frm.serialize();
    
    	$.ajax({ 
              type : "GET",
              url:"roomAjaxMng.do",
              dataType:"json",
         	  data :  dataString,
              success : function(data) {
            	  var output = '';
    		  			output += '<table>';
    		  			output += '<tr id="thfix">                 ';
    		      		output += '	<th> ';
    		      		output += '		선택              ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		강의실 코드          ';
    		      		output += '	</th>               ';
    		      		output += '	<th> ';
    		      		output += '		강의실 이름            ';
    		      		output += '	</th>               ';
    		      		output += '</tr>                ';
    		      	
    		      	  for(var i=0; i<data.result.length; i++) {
    		      	      var obj = data.result[i];

    						output += '<tr>';
    						output += '<td width=50><input type=\"checkbox\" id=\"chk\" name=\"chk\" value=' + obj.code +'/></td>';
    						output += '<td><a href=\"#\" name=\"detail\" id=\"detail\" onclick=\"fn_detailRoom(\''+obj.code+'\');\">' + obj.code + '</a></td>';
    						output += '<td>' + obj.name + '</td>';
    						output += '</tr>';
    						
    		      	  }
    		  	  output += '</table>';
    		  	  $('#output').append(output);
    		  	

              },
              complete : function(data) {
              },
              error : function(xhr, status, error) {
                    alert("에러발생" + status + ','+ error + ','+ xhr);
              }
        });
    	
    }

    function fn_dupValidate(){    
    	$.ajax({ 
            type : "GET",
            url:"roomdupMng.do?code=" + document.form.code.value ,
            success : function(data) {
            	if(data == 'Y'){
            		document.form.code.value = "";
            		document.form.code.focus();
            		alert("강의실 코드가 중복되었습니다.");
            		return;
            	}else{
            		fn_saveRoom();
            	}
            },
            complete : function(data) {
            },
            error : function(xhr, status, error) {
                  alert("에러발생" + status + ','+ error + ','+ xhr);
            }
      });	
    }


	function fn_validate(){

		var form = document.form;
		
		if(form.code.value == ''){
			form.code.focus();
			return false;
		}
		
		if(form.name.value == ''){
			form.name.focus();
			return false;
		}
	
		return true;
	}
	
	function fn_saveRoom(){

		var form =  document.form;

			form.action = "roomInsertMng.do";
			form.submit();
		
	}
	
	function fn_delRoom(){

			var form =document.form;
			var cnt = 0;
			var strDel = "";
			
			for(var i =0 ; i<form.chk.length; i++)
			{
			  if(form.chk[i].checked){
				  strDel = strDel + form.chk[i].value + '/';  
				  cnt++;
			  }	
			}
					
			if(cnt == 0 ){
				alert("삭제 할 항목을 선택해 주세요");
				return;
			}
			
			form.strdelete.value = strDel; 
			form.action = "roomDeleteMng.do";
			form.submit();
	}		
	
	function fn_detailRoom(obj){
			
		var form = document.form;
		form.strcode.value = obj; 
		form.action = "roomDetailMng.do";
		form.submit();
	}

	function fn_listRoom(){
		
		var form = document.form;
		form.action = "roomMng.do";
		form.submit();
	}	

	function fn_back(){

		var form = document.form;
		alert("리스트화면으로 이동합니다.");
		form.action = "roomMng.do";
		form.submit();
	}

</script>
<body onload="init();">
<form name="form" method="post" >
	<input type="hidden" name ="strdelete" id="strdelete" value="">
	<input type="hidden" name ="strcode" value="">
	<input type="hidden" name ="hidencode" value="${vo.code}">
<table class="table1">
	<tr>
		<td>
			<table class="table2">
				<tr>
					<th>
					<th>
						강의실 코드
					</th>
					<td>
						<INPUT TYPE="text" NAME="code" id="code" value='${vo.code}' maxlength=5 >
					<td>
					<th>
					</th>
					<th>
						강의실 이름
					</th>
					<td>
						<INPUT TYPE="text" NAME="name" value='${vo.name}'>
					</td>
			    </tr>
			   
			    <tr>
					<td colspan= 4>
						<input type="button" id="save" value="저장"  /> 														
						<input type="button" id="delete" value="삭제"  /> 
						<input type="button" id="search" value="조회"  />
						<input type="button" id="exit" value="리스트보기" onclick="fn_back()" />
					</td>
				</tr>

			</table>
			<h2>강의실 목록</h2>
			<div id="output" class="defTable"></div> 
		</td>
	</tr>
</table>

 </form>

</body>
</html>
 